package com.moneywelcome.orderassembly.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.orderassembly.model.DividendRecord;
import com.moneywelcome.orderassembly.mapper.DividendRecordMapper;
import com.moneywelcome.orderassembly.model.RedeemRecord;
import com.moneywelcome.orderassembly.service.DividendRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moneywelcome.orderfacade.dto.DividendOrderDto;
import com.moneywelcome.orderfacade.dto.RedeemOrderDto;
import com.moneywelcome.orderfacade.vo.DividendRecordVo;
import com.moneywelcome.orderfacade.vo.OrderPageVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.StatusCode;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 分红记录表 服务实现类
 * </p>
 *
 * @author alice
 * @since 2022-01-06
 */
@Service
public class DividendRecordServiceImpl extends ServiceImpl<DividendRecordMapper, DividendRecord> implements DividendRecordService {
    @Override
    public DividendOrderDto create(DividendRecordVo dividendRecordVo) {
        DividendRecord dividendRecord = BeanUtil.copyProperties(dividendRecordVo, DividendRecord.class);
        dividendRecord.setDividendTime(DateUtil.now());
        dividendRecord.setDividendSerial(UUID.randomUUID().toString());
        if (!save(dividendRecord)) throw new MwcArgsException("分红纪录生成失败", StatusCode.Error);
        return BeanUtil.copyProperties(dividendRecord,DividendOrderDto.class);
    }

    @Override
    public PageResult<DividendOrderDto> queryRecord(OrderPageVo op) {
        QueryWrapper<DividendRecord> qw = new QueryWrapper<>();
        if (null!= op.getUserId())  qw.eq("user_id", op.getUserId());
        if (null!=op.getFinancialId()) qw.eq("financial_id",op.getFinancialId());
        if (null != op.getStartTime()) qw.ge("dividend_time", op.getStartTime());
        if (null != op.getEndTime()) qw.le("dividend_time", op.getEndTime());
        String keyword = op.getKeyword();
        if (!StrUtil.hasEmpty(keyword)) {
            qw.and(qwt -> {
                qwt.like("varidividend_serialety", keyword);
            });
        }
        Page<DividendRecord> page = page(new Page<>(op.getCurrent(), op.getSize()),qw);
        PageResult pageResult = BeanUtil.copyProperties(page, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(page.getRecords(), DividendOrderDto.class));
        pageResult.setTotalCount(page.getTotal());
        return pageResult;
    }
}
